home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_01 / allison / swap.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-07  |  549 b   |  37 lines

  1. // swap.cpp
  2. #include <iostream.h>
  3.  
  4. template<class T>
  5. void swap(T& x, T& y)
  6. {
  7.     T temp = x;
  8.     x = y;
  9.     y = temp;
  10. }
  11.  
  12. main()
  13. {
  14.     int a = 1, b = 2;
  15.     double c = 1.1, d = 2.2;
  16.     char *s = "hello", *t = "there";
  17.  
  18.     swap(a,b);
  19.     cout << "a = " << a << ", b = " << b << '\n';
  20.  
  21.     swap(c,d);
  22.     cout << "c = " << c << ", d = " << d << '\n';
  23.  
  24.     swap(s,t);
  25.     cout << "s = " << s << ", t = " << t << '\n';
  26.  
  27.     return 0;
  28. }
  29.  
  30. /* Output:
  31. a = 2, b = 1
  32. c = 2.2, d = 1.1
  33. s = there, t = hello
  34.  
  35. // End of File
  36.  
  37.